package dwl.graduation.design.service.impl;

import dwl.graduation.design.domain.*;
import dwl.graduation.design.mapper.PermissionMapper;
import dwl.graduation.design.mapper.RoleMapper;
import dwl.graduation.design.mapper.RolePermissionMapper;
import dwl.graduation.design.mapper.UserRoleMapper;
import dwl.graduation.design.service.RoleService;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public boolean insertRoleInfo(RoleInsertRequest request) {
        Role role = new Role();
        BeanUtils.copyProperties(request, role);
        int flag = roleMapper.insertSelective(role);
        return flag > 0;
    }

    @Override
    public PageResult findRoleList(QueryPageBean queryPageBean) {
        if (!Objects.isNull(queryPageBean.getPageSize()) && !Objects.isNull(queryPageBean.getCurrentPage())) {
            queryPageBean.setCurrentPage((queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize());
        }
        List<Role> roleList = roleMapper.selectRoleList(queryPageBean);
        Long count = roleMapper.getListCount(queryPageBean);
        return PageResult.builder().rows(roleList).total(count).build();
    }

    @Override
    public void deleteRole(Integer id) {
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(id);
        List<RolePermissionKey> list = rolePermissionMapper.selectByExample(rolePermissionExample);
        if(list.size()>0){
            rolePermissionMapper.deleteByExample(rolePermissionExample);
        }
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean updateRole(RoleUpdateRequest roleUpdateRequest) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdEqualTo(roleUpdateRequest.getId());
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        if (roleList.size() == 0) {
            return false;
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateRequest, role);
        RoleExample roleUpdateExample = new RoleExample();
        roleUpdateExample.createCriteria().andIdEqualTo(role.getId());
        int flag = roleMapper.updateByExampleSelective(role, roleUpdateExample);
        return flag > 0;
    }

    @Override
    public Set<Role> getUserRole(Integer userId) {
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(userId);
        List<UserRoleKey> userRoleKeys = userRoleMapper.selectByExample(userRoleExample);
        List<Integer> roleIdList = userRoleKeys.stream().map(UserRoleKey::getRoleId).collect(Collectors.toList());
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(roleIdList);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        roles.parallelStream().forEach(item->{
            List<Integer> permissionId = rolePermissionMapper.getPermissionId(item.getId());
            PermissionExample permissionExample = new PermissionExample();
            permissionExample.createCriteria().andIdIn(permissionId);
            List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);
            item.setPermissions(new HashSet<>(permissionList));
        });
        return new HashSet<>(roles);
    }

    @Override
    public boolean insertPermissionForRole(InsertPermissionForRoleRequest request) {
        RolePermissionKey rolePermissionKey = new RolePermissionKey();
        BeanUtils.copyProperties(request, rolePermissionKey);
        int flag = rolePermissionMapper.insertSelective(rolePermissionKey);
        return flag > 0;
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleMapper.findRoleById(id);
    }
}
